package com.waypolice.manager.controller;

import com.waypolice.base.BaseController;
import com.waypolice.common.utils.MathUtils;
import com.waypolice.manager.service.ActDefineService;
import com.waypolice.manager.service.UpmsDictionaryService;
import com.waypolice.pojo.UpmsActDefine;
import com.waypolice.pojo.UpmsDictionary;
import com.waypolice.pojo.UpmsDictionaryExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 描述:
 *
 * @author zyp
 * @description 工作流定义控制器
 * @create 2019-02-26 11:31
 */
@Controller
@Api(value = "工作流定义控制器", description = "工作流定义的添加、修改、删除")
@RequestMapping(value="/pcwindow")
public class ActDefineAction extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ActDefineAction.class);

    @Autowired
    public RepositoryService repositoryService;

    @Autowired
    public ActDefineService actDefineService;

    @Autowired
    public UpmsDictionaryService upmsDictionaryService;

    /**
     * @author:zyp
     * @description 工作流定义跳转
     * @date: 2019/2/26 11:37
     * @return 
     */
    @ApiOperation(value = "工作流定义跳转")
    @RequiresPermissions("upms:define:read")
    @RequestMapping(value = "/toActDefine", method = RequestMethod.GET)
    public String toActModel(HttpServletResponse response,
                             HttpServletRequest request,
                             ModelMap modelMap
    ) {
        //工作流字典项
        List<UpmsDictionary> lists = upmsDictionaryService.getUpmsDictionaryByCode("activitiCode");
        //当前工作流部署的唯一标识KEY
        Set<String> keySet = new HashSet<String>();
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
        for (ProcessDefinition processDefinition:processDefinitionList){
            keySet.add(processDefinition.getKey());
        }
        modelMap.put("lists",lists);
        modelMap.put("keySet",keySet);
        return "/activitiManager/actDefine";
    }

    /**
     * @author:zyp
     * @description 工作流定义列表
     * @date: 2019/2/26 13:38
     * @return 
     */
    @ApiOperation(value = "工作流定义列表")
    @RequiresPermissions("upms:define:read")
    @RequestMapping(value = "/getActDefine")
    @ResponseBody
    public Object defineList(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                             @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                             @RequestParam(required = false, defaultValue = "", value = "search") String search,
                             @RequestParam(required = false, value = "sort") String sort,
                             @RequestParam(required = false, value = "order") String order
    ) {
        Object object = actDefineService.actDefineList(offset,limit,sort,order,search);
        return object;
    }

    /**
     * @author:zyp
     * @description 工作流定义添加
     * @date: 2019/2/26 16:01
     * @return 
     */
    @ApiOperation(value = "工作流定义添加")
    @RequiresPermissions("upms:define:create")
    @RequestMapping(value = "/addDefine")
    @ResponseBody
    public String addDefine(UpmsActDefine actDefine){
        long adStatus = actDefine.getAdStatus();
        //当新加入的工作流标识为正常时，则之前的工作流作废，沿用此工作流
        if(adStatus==1){
            int updateNum = actDefineService.updateStatusByAdCode(actDefine.getAdCode());
        }
        int count = actDefineService.insert(actDefine);
        if(count==1){
            return "1";
        }else{
            return "0";
        }
    }

    /**
     * @author:zyp
     * @description 工作流定义修改回显数据
     * @date: 2019/2/26 16:01
     * @return 
     */
    @ApiOperation(value = "工作流定义修改（回显修改数据）")
    @RequiresPermissions("upms:define:update")
    @RequestMapping(value = "/toUpdateDefine")
    @ResponseBody
    public Object toUpdateDefine(String id, ModelMap modelMap){
        //工作流定义内容
        UpmsActDefine actDefine = actDefineService.getActDefineById(MathUtils.string2int(id));
        return actDefine;
    }

    /**
     * @author:zyp
     * @description 工作流定义修改
     * @date: 2019/2/26 16:01
     * @return 
     */
    @ApiOperation(value = "工作流定义修改（修改操作）")
    @RequiresPermissions("upms:define:update")
    @RequestMapping(value = "/updateDefine")
    @ResponseBody
    public String updateDefine(UpmsActDefine actDefine){
        long adStatus = actDefine.getAdStatus();
        //当新加入的工作流标识为正常时，则之前的工作流作废，沿用此工作流
        if(adStatus==1){
            actDefineService.updateStatusByAdCode(actDefine.getAdCode());
        }
        int count = actDefineService.updateByPrimaryKey(actDefine);
        if(count==1){
            return "1";
        }else{
            return "0";
        }
    }

    /**
     * @author:zyp
     * @description 删除工作流定义
     * @date: 2019/2/26 16:02
     * @return 
     */
    @ApiOperation(value = "工作流定义删除")
    @RequiresPermissions("upms:define:delete")
    @RequestMapping(value = "/delDefine")
    @ResponseBody
    public String delDefine(String ids){
        if (!StringUtils.isBlank(ids)) {
            String[] idArray = ids.split("-");
            for (String id : idArray) {
                if (StringUtils.isBlank(id)) {
                    continue;
                }
                actDefineService.deleteDefineById(MathUtils.string2int(id));
            }
        }
        return "1";
    }

}